package myky.com.doctorapp.presenter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import myky.com.doctorapp.api.ResponseTransformer;
import myky.com.doctorapp.entity.SearchHistoryItem;
import myky.com.doctorapp.api.schedulers.SchedulerHelper;
import myky.com.doctorapp.base.BasePresenterIm;
import myky.com.doctorapp.base.SimpleSubscribe;
import myky.com.doctorapp.contract.SearchContract;
import myky.com.doctorapp.model.SearchModel;

public class SearchPresenter extends BasePresenterIm<SearchContract.View> implements SearchContract.Presenter {

    SearchModel searchModel;

    public SearchPresenter() {
        searchModel = new SearchModel();
    }

    @Override
    public void getSearchHistory(String version, String searchScope, String customId) {
        searchModel.findAllSearchHistory(version, searchScope, customId)
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerHelper.applySchedulers())
                .subscribe(new SimpleSubscribe<ArrayList<SearchHistoryItem>>() {
                    @Override
                    protected void onSuccess(ArrayList<SearchHistoryItem> searchHistoryItem) {
                        mView.showAllHistory(searchHistoryItem);
                    }

                    @Override
                    protected void onFailed(String msg) {
                        mView.logCat(msg);

                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                        addSubscription(d);

                    }
                });
    }

    @Override
    public void deleteSingleHistoryById(int id, String version, String customId,int postion) {
        
        searchModel.deleteSingleHistoryById(id, version, customId).compose(ResponseTransformer.handleResult())
                .compose(SchedulerHelper.applySchedulers())
                .subscribe(new SimpleSubscribe<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscription(d);

                    }

                    @Override
                    protected void onSuccess(Object o) {

                        mView.deleteSingleHistorySucced(postion);

                    }

                    @Override
                    protected void onFailed(String msg) {
                        mView.logCat(msg);

                    }
                });
    }

    @Override
    public void deleteAllHistoryByUserId(String version, String customId) {

    }

    @Override
    public void findKeyWordByInput(String version, String searchType, String input) {
        searchModel.findKeyWordByInput(version, searchType, input)
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerHelper.applySchedulers())
                .subscribe(new SimpleSubscribe<List<String>>() {
                    @Override
                    protected void onSuccess(List<String> keyWordEntity) {
                        mView.showReleaedKeyWord(keyWordEntity);
                    }

                    @Override
                    protected void onFailed(String msg) {
                        mView.logCat(msg);
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscription(d);
                    }
                });
    }
}